#ifdef _PLATFORM_WII
#include "window_handler_wii.h"

#include "wiigraphics/wii_graphics.h"

#include "input/wiimote.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "ui/ui_manager.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "audio/audio.h"
#include "resman/resman.h"
#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"	

#include <revolution.h>
#include <demo.h>
void		UpdateHBM();
void		ShutdownHBM();
void		StartupHBM();


WindowHandlerWii::WindowHandlerWii():
					m_ShowCustomMouse(true),
					m_EnableHBM(false)
{
 	// Init DVD
	DVDInit();
	// Init R/W memory
	int rv = NANDInit();
	
	// init screen here (for now). Replace with non-demo implementation
//	DEMOInit(&GXEurgb60Hz480Int);    // Init os, pad, gx, vi

	if (rv != NAND_RESULT_OK)
	{
		OSReport("NANDInit failed: %d\n", rv);
		OSHalt("Cannot continue\n");
	}
 }


WindowHandlerWii::~WindowHandlerWii()
{
}

bool		WindowHandlerWii::InitWindow(const std::string& Name, const std::string& Title, int	X, int Y, int Width, int Height) 
{
	m_Title = Title;
	m_AppName = Name;
	m_Width = Width;
	m_Height = Height;
	
	m_IsPaused = false;
	m_HasFocus = true;

	m_x			 = (m_OrigWidth -Width)/2;	
    m_y			 = (m_OrigHeight -Height)/2;			
    m_BaseWidth = m_Width = Width;
    m_BaseHeight = m_Height = Height;	
    
  
    
	GraphicsUtil::SetWindowSize(Width, Height);
	
	
	return true;
}


bool		WindowHandlerWii::InitGraphics()
{ 	
	WiiGraphics::Create();   
	WiiGraphics*	pG = GetWiiGraphics();
	pG->Init();
	//pG->InitWindow(640,480,true, false);    

	return true;
	
}


void		WindowHandlerWii::SetHBM(bool b)
{
	if (b==m_EnableHBM) return;
	m_EnableHBM = b;
	if (m_EnableHBM)
	{
		StartupHBM();
	}
	else
	{
		ShutdownHBM();
	}
}

bool		WindowHandlerWii::DestroyWindow() 
{
	m_IsPaused = true;
	m_IsDestroyed = true;
	return true;
}

bool		WindowHandlerWii::Tick(float dt)
{
	input::Wiimote::Instance().Tick(dt);
	
	if (m_EnableHBM)
	{
		m_IsPaused = true;
		UpdateHBM();
	}
	else
	{
		m_IsPaused = false;
	}
	
	return true;
	
}

void DELEGATE_CALL	WindowHandlerWii::OnHardQuit(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	DestroyWindow();	
}


void		WindowHandlerWii::ResetDevice()
{
	
}

bool		WindowHandlerWii::UpdateOS()
{
	return true;
}


//not applicable to all, but for most
bool		WindowHandlerWii::Minimize()
{
	return true;
}

bool		WindowHandlerWii::Maximize()
{
	return true;
}

void		WindowHandlerWii::SwitchFullscreen(int Width, int Height) 
{
}

void		WindowHandlerWii::SwitchWindowed(int Width, int Height)
{
}

std::string	WindowHandlerWii::GetSaveFolder()
{	
    char homePathBase[NAND_MAX_PATH]="";
    NANDGetHomeDir(homePathBase);
    return (homePathBase);
}

bool		WindowHandlerWii::OnGainFocus()
{
	return true;
}

bool		WindowHandlerWii::OnLooseFocus()
{
	return true;
}


// functions to change screen resolution around
bool		WindowHandlerWii::GetCurrentResolution(unsigned int& Width, unsigned int& Height, unsigned int &Depth)
{
	return true;
}

bool		WindowHandlerWii::SetResolution(unsigned int Width, unsigned int Height, unsigned int Depth)
{
	return true;
}

bool		WindowHandlerWii::DetectWidescreenMode()
{
	return true;
}

bool		WindowHandlerWii::RestoreResolution()
{
	return true;
}

#endif
